package com.ystarkotlin.utils

import android.content.Context
import android.widget.ImageView
import androidx.annotation.DrawableRes
import com.blankj.utilcode.util.ConvertUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.Priority
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.RequestOptions.bitmapTransform
import com.socks.library.KLog
import com.ystarkotlin.R
import jp.wasabeef.glide.transformations.BlurTransformation
import jp.wasabeef.glide.transformations.RoundedCornersTransformation
import java.io.File

/**
 * Created by Ystar
 *Data 2019/9/4
 */
class ImgUtils {

    companion object {
        val Defult_Type = 0 //默认
        val CircleCrop_Type = 1  //圆形
        val RoroundConner_Type = 2  //圆角
        val Blur_Type = 3  //模糊
        val Gif_TYPe = 4  //模糊
        fun geturl(url: String): String {
            //return ApiService.SERVER_URL+url; 如果需要用baseurl
            return url
        }
/*加载网络*/
        fun load(context: Context, url: String, view: ImageView) {
            KLog.a("url:" + geturl(url))
            Glide.with(context)
                .load(geturl(url))
                .apply(getRequestOptions())
                .into(view)
        }
        /*加载文件*/
        fun load(context: Context, url: File, view: ImageView) {
            Glide.with(context)
                .load(url)
                .apply(getRequestOptions())
                .into(view)
        }

     /*加载项目本地*/
        fun load(context: Context, @DrawableRes res: Int, view: ImageView) {
            Glide.with(context)
                .load(res)
                .apply(getRequestOptions())
                .into(view)
        }
        /**
         * 加载圆角
         *
         * @param context
         * @param url
         * @param view
         * @param roroundConner 圆角大小
         */
        fun load_roundcorner(context: Context, url: String, view: ImageView, roroundConner: Int) {
            Glide.with(context)
                .load(geturl(url))
                .apply(getRequestOptions(RoroundConner_Type, roroundConner))
                .into(view)
        }


        fun getRequestOptions(): RequestOptions {
            val requestOptions = RequestOptions()
            requestOptions
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC) //表示让Glide根据图片资源智能地选择使用哪一种缓存策略（默认选项）。
                .placeholder(R.mipmap.no_data)//图片加载出来前，显示的图片
                .fallback(R.mipmap.no_data) //url为空的时候,显示的图片
                .error(R.mipmap.no_data)//图片加载失败后，显示的图片
            return requestOptions
        }

        /**
         * @param type
         * @param options RoroundConner_Type：圆角大小 Blur_Type: 模糊程度
         * @return
         */
        fun getRequestOptions(type: Int, options: Int): RequestOptions {
            val requestOptions = getRequestOptions()

            if (type == RoroundConner_Type) {
                return setroundCounerRequestOptions(requestOptions, options)

            } else if (type == Blur_Type) {
                KLog.a("模糊")
                return setBlurRequestOptions(requestOptions,options)}
            return requestOptions
        }

        fun getRequestOptions(
            type: Int,
            roundConner: Int,
            round_type: RoundedCornersTransformation.CornerType
        ): RequestOptions {
            val requestOptions = getRequestOptions()

            return if (type == RoroundConner_Type) {
                setroundCounerRequestOptions(requestOptions, roundConner)
            } else requestOptions
        }


        /**
         * 设置圆角
         *
         * @param requestOptions
         * @param roundConer
         * @return
         */
        private fun setroundCounerRequestOptions(requestOptions: RequestOptions, roundConer: Int): RequestOptions {
            requestOptions
                .apply(
                    bitmapTransform(
                        RoundedCornersTransformation(
                            ConvertUtils.dp2px(roundConer.toFloat()),
                            0,
                            RoundedCornersTransformation.CornerType.ALL
                        )
                    )
                )

            return requestOptions
        }

        /**
         * bluroption 模糊程度
         */
        private fun setBlurRequestOptions(requestOptions: RequestOptions, bluroption: Int): RequestOptions {
            requestOptions
                .apply(
                    bitmapTransform(
                        BlurTransformation(
                            bluroption
                        )
                    )
                )

            return requestOptions
        }

    }


}